/**************************************************
 * 上海美峰数码科技有限公司(http://www.morefuntek.com)
 * 模块名称: StorgeAndTrade
 * 功能描述：
 * 文件名：StorgeAndTrade.java
 **************************************************
 */
package com.ywh.log;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 功能描述
 * 
 * @author yaowenhao
 * @since 2011-5-4
 * @version 1.0
 */
public class StorgeAndTrade {
	private Map<String, List<Long>> tradeTimes = new HashMap<String, List<Long>>();
	private Map<String, List<Long>> storgeTimes = new HashMap<String, List<Long>>();

	public void readtradeLog(String tradeLog) throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(tradeLog));
		String line = null;
		while ((line = br.readLine()) != null) {
			int start = line.indexOf("方[");
			int end = line.indexOf("|", start + 1);
			if (start != -1 && end != -1) {
				String[] allStr = line.split(" ");
				String dateStr = allStr[0];
				String timeStr = allStr[1];
				Long time = parse(dateStr + " " + timeStr);
				String mStr = line.substring(start + 2, end);
				if (mStr.startsWith("★")) {
					mStr = mStr.substring(1);
				}
				List<Long> times = tradeTimes.get(mStr);
				if (times == null) {
					times = new ArrayList<Long>();
					times.add(time);
					tradeTimes.put(mStr, times);
				} else {
					times.add(time);
				}
			}
		}

		if (br != null)
			br.close();
	}

	public void readstorgeLog(String storgeLog) throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(storgeLog));
		String line = null;
		while ((line = br.readLine()) != null) {
			int start = line.indexOf("家[");
			int end = line.indexOf("]", start + 1);
			if (start != -1 && end != -1) {
				String[] allStr = line.split(" ");
				String dateStr = allStr[0];
				String timeStr = allStr[1];
				Long time = parse(dateStr + " " + timeStr);
				String mStr = line.substring(start + 2, end);
				if (mStr.startsWith("★")) {
					mStr = mStr.substring(1);
				}
				// System.out.println(mStr);
				List<Long> times = storgeTimes.get(mStr);
				if (times == null) {
					times = new ArrayList<Long>();
					times.add(time);
					storgeTimes.put(mStr, times);
				} else {
					times.add(time);
				}
			}
		}
		// for(Entry<String, List<Long>> sub : storgeTimes.entrySet()) {
		// System.out.println(sub);
		//
		// }

		if (br != null)
			br.close();
	}

	public void analyse(PrintWriter pw) {
		Map<String, List<String>> result = new HashMap<String, List<String>>();
		for (Entry<String, List<Long>> trade : tradeTimes.entrySet()) {
			String name = trade.getKey();
			List<Long> tradeTime = trade.getValue();
			List<Long> storageTime = storgeTimes.get(name);
			if (storageTime == null)
				continue;
			for (int i = 0; i < tradeTime.size(); i++) {
				for (int j = 0; j < storageTime.size(); j++) {
					long tt = tradeTime.get(i);
					long st = storageTime.get(j);
					if (Math.abs((tt - st) / 1000.0) < 5) {
						Date ttD = new Date(tt);
						Date stD = new Date(st);
						List<String> info = result.get(name);
						if (info == null) {
							info = new ArrayList<String>();
							info.add("交易时间:" + DateUtil.format(ttD)
									+ ",仓库存取时间:" + DateUtil.format(stD));
							result.put(name, info);
						} else {
							info.add("交易时间:" + DateUtil.format(ttD)
									+ ",仓库存取时间:" + DateUtil.format(stD));
						}
					}
				}
			}
		}
		for (Entry<String, List<String>> sub : result.entrySet()) {
			// System.out.println(sub);
			pw.println(sub);
		}
	}

	private Long parse(String dateTimeString) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		Date date = new Date();
		try {
			date = df.parse(dateTimeString);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date.getTime();
	}

	public static void main(String[] args) {
		try {
			StorgeAndTrade sat = new StorgeAndTrade();
			sat.readtradeLog("log/2011-04-29tradeInfo.log");
			sat.readstorgeLog("log/2011-04-29StorageItem.log");
			sat.analyse(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
